package com.moleq.base;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public abstract class BaseDBService<T> {

	private MQConnection mConn;
	private SQLiteDatabase mDb;

	public BaseDBService(MQConnection conn) {
		mConn = conn;
		mDb = mConn.getDB();
	}

	public abstract ArrayList<T> findAll(T obj);

	public abstract boolean isExist(T obj);

	public long getCount(String tableName) {
		long count = 0;
		if (mConn.isConnected()) {
			Cursor cursor = mDb.rawQuery("SELECT COUNT(*) FROM " + tableName, null);
			if (cursor.moveToFirst()) {
				count = cursor.getInt(0);
			}
			cursor.close();

		}
		return count;
	}

	public abstract boolean update(T obj);

	public abstract boolean insert(T obj);

	public abstract boolean delete(T obj);

	public boolean insertOrUpdate(T obj) {
		boolean result = false;
		if (isExist(obj)) {
			result = insert(obj);
		} else {
			result = insertOrUpdate(obj);
		}
		return result;
	}
}
